{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "def func(x):\n",
    "    time.sleep(1)\n",
    "    print(f\"Heavy operation for {x}\")\n",
    "    return x * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Heavy operation for 1\n",
      "Func returned: 10\n",
      "Heavy operation for 1\n",
      "Func returned: 10\n"
     ]
    }
   ],
   "source": [
    "print(\"Func returned:\", func(1))\n",
    "print(\"Func returned:\", func(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "import functools\n",
    "import time\n",
    "\n",
    "@functools.lru_cache()\n",
    "def func(x):\n",
    "    time.sleep(1)\n",
    "    print(f\"Heavy operation for {x}\")\n",
    "    return x * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Heavy operation for 1\n",
      "Func returned: 10\n",
      "Func returned: 10\n",
      "Heavy operation for 2\n",
      "Func returned: 20\n"
     ]
    }
   ],
   "source": [
    "print(\"Func returned:\", func(1))\n",
    "print(\"Func returned:\", func(1))\n",
    "print(\"Func returned:\", func(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "import functools\n",
    "import time\n",
    "\n",
    "@functools.lru_cache(maxsize=2)\n",
    "def func(x):\n",
    "    time.sleep(1)\n",
    "    print(f\"Heavy operation for {x}\")\n",
    "    return x * 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Heavy operation for 1\n",
      "Func returned: 10\n",
      "Heavy operation for 2\n",
      "Func returned: 20\n",
      "Heavy operation for 3\n",
      "Func returned: 30\n",
      "Func returned: 30\n",
      "Func returned: 20\n",
      "Heavy operation for 1\n",
      "Func returned: 10\n"
     ]
    }
   ],
   "source": [
    "print(\"Func returned:\", func(1))\n",
    "print(\"Func returned:\", func(2))\n",
    "print(\"Func returned:\", func(3))\n",
    "print(\"Func returned:\", func(3))\n",
    "print(\"Func returned:\", func(2))\n",
    "print(\"Func returned:\", func(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "import functools\n",
    "import time\n",
    "\n",
    "def func(x):\n",
    "    time.sleep(1)\n",
    "    print(f\"Heavy operation for {x}\")\n",
    "    return x * 10\n",
    "\n",
    "cached_func = functools.lru_cache()(func)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Heavy operation for 1\n",
      "Cached func returned: 10\n",
      "Cached func returned: 10\n",
      "Heavy operation for 1\n",
      "Func returned: 10\n",
      "Heavy operation for 1\n",
      "Func returned: 10\n"
     ]
    }
   ],
   "source": [
    "print(\"Cached func returned:\", cached_func(1))\n",
    "print(\"Cached func returned:\", cached_func(1))\n",
    "print(\"Func returned:\", func(1))\n",
    "print(\"Func returned:\", func(1))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3+"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
